/*
 * DRadarViewの描画を行うスレッド
 */
package com.hackathon;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;

public class SurfaceThread extends Thread{

	private boolean halt;
	
	private DRadarView mView;
	private int deg;
	private int count;
	private int gridWidth, gridHeight;
	
	public SurfaceThread(DRadarView view){
		halt = false;
		mView = view;
		deg = 0;
		count = 0;
		
		gridWidth = mView.getWidth() / 8 / 2;
		gridHeight = mView.getHeight() / 8 / 2;
	}
	
	@Override
	public void run(){
		while( !halt ){
			Canvas canvas = mView.getHolder().lockCanvas();
			
			canvas.drawColor(Color.BLACK);
			drawGrid(canvas);
			drawBalls(canvas);
			drawTriangle(canvas);
			drawRotateArc(canvas);

			mView.getHolder().unlockCanvasAndPost(canvas);
		}
	}
	
	//安全にスレッドを停止するためのメソッド
	public void halt(){
		halt = true;
		interrupt();
	}
	
	private void drawGrid(Canvas canvas){
		Paint paint = new Paint();
		paint.setARGB(255, 0, 100, 0);
		paint.setStrokeWidth(2);
		paint.setStyle(Paint.Style.STROKE);
		
		int width = mView.getWidth();
		int height = mView.getHeight();
		
		//XY lines
		canvas.drawLine(width/2, 0, width/2, height, paint);
		canvas.drawLine(0, height/2, width, height/2, paint);
		
		//grid lines
		int gridWidth1 = gridWidth;
		int gridHeight1 = gridHeight;
		int numX = width / gridWidth1 / 2;
		int numY = height / gridHeight / 2;
		if(mView.isZoom()){
			gridWidth1 *= 2;
			gridHeight1 *= 2;
			numX /= 2;
			numY /= 2;
		}
		paint.setStrokeWidth(1);
		for(int i=0; i<=numX; i++) {
			canvas.drawLine( width / 2 + gridWidth1*i, 0, width / 2 + gridWidth1*i, height, paint);
			canvas.drawLine( width / 2 - gridWidth1*i, 0, width / 2 - gridWidth1*i, height, paint);
		}
		for(int i=0; i<=numY; i++){
			canvas.drawLine(0, height / 2 + gridHeight1*i, width, height / 2 + gridHeight1*i, paint);
			canvas.drawLine(0, height / 2 - gridHeight1*i, width, height / 2 - gridHeight1*i, paint);
		}
	}
	
	private void drawBalls(Canvas canvas){
		
		double x[] = new double [7];
		double y[] = new double [7];
		Hubeny hubeny = new Hubeny();
		Point[] pts = hubeny.execute();
		for(int i=0; i<DragonData.NUM_BALL; i++){
			
			if(mView.isZoom()){
				x[i] = (pts[i].getPointX()*2.0 + 1.0) / 2.0;
    			y[i] = (pts[i].getPointY()*2.0 + 1.0) / 2.0;
    		}else{
    			x[i] = (pts[i].getPointX() + 1.0) / 2.0;
    			y[i] = (pts[i].getPointY() + 1.0) / 2.0;
    		}
		}

		Paint paint = new Paint();
		int width = mView.getWidth();
		int height = mView.getHeight();
		paint.setARGB(255 - (count % 255), 200, 150, 0);
		paint.setStyle(Paint.Style.FILL);
		for(int i = 0; i < DragonData.NUM_BALL ; i++) {
			canvas.drawCircle( width*(float)x[i], height*(float)y[i], 5, paint);   		
		}

		count += 4;
	}
	
	//画面中央に三角形を描画する
	private void drawTriangle(Canvas canvas){

		Paint paint = new Paint();
		paint.setColor(Color.RED);
		paint.setStyle(Paint.Style.FILL);
		paint.setStrokeWidth(3);
		
		float cx = mView.getWidth() / 2.0f;
		float cy = mView.getHeight() / 2.0f;

		canvas.drawLine(
				cx,cy-10, 	
				(float)(cx+5*Math.sqrt(3.0)), cy+5, 
				paint);
		canvas.drawLine(
				(float)(cx+5*Math.sqrt(3.0)), cy+5, 	
				(float)(cx-5*Math.sqrt(3.0)), cy+5, 
				paint);
		canvas.drawLine(
				cx,cy-10,	
				(float)(cx-5*Math.sqrt(3.0)), cy+5, 
				paint);
	}
	
	//draw rotate arc
	private void drawRotateArc(Canvas canvas){

		Paint paint = new Paint();
		//paint.setColor(Color.YELLOW);
		paint.setARGB(64, 255, 255, 0);
		paint.setStyle(Paint.Style.FILL);
		paint.setStrokeWidth(2);
		
		int cx = mView.getWidth() / 2;
		int cy = mView.getHeight() / 2;
		int r = mView.getHeight() / 2;
		RectF rect = new RectF(cx-r*2, cy-r*2, cx+r*2, cy+r*2);
		canvas.drawArc(rect, deg, 30, true, paint);
		
		deg += 5;
		if(deg > 360){
			deg -= 360;
		}

	}

}
